﻿using System;
using System.Collections.Generic;
using System.Linq;

namespace Week4_work1
{
    class Program
    {
        static void Main(string[] args)
        {
            OrderService a = new OrderService();
            a.addOrder(0001, "苹果", "甲", 10);
            a.addOrder(0002, "香蕉", "乙", 8);
            a.addOrder(0003, "西瓜", "丙", 15);
            a.addOrder(0004, "凤梨", "丁", 13);
            a.display();
            Console.WriteLine("删除一项后：");
            a.deleteOrder(a.searchByItem("西瓜"));
            a.display();
            Console.WriteLine("修改一项后");
            a.rchangeOrder(a.searchByID(1), 0001, "葡萄", "甲", 20);
            a.display();
            Console.WriteLine();
            a.sort();
            Console.WriteLine("排序之后");
            a.display();
        }
    }
    class Order
    {
    public int ID;         //订单编号
    public int orderID
        {
            get { return ID; }
            set { if (value >= 0) { ID = value; }
                else { ID = 0; }
            }
        }
        public OrderDetails od = new OrderDetails();
     
        public override bool Equals(object obj)
        {
            Order a = obj as Order;
            return this.ID == a.ID;
        }
        public override int GetHashCode()
        {
            return ID;
        }
    }
    class OrderDetails {
        public int price;
        public Client user = new Client();
        public OrderItem Item = new OrderItem();
        public int Price
        {
            get { return price; }
            set
            {
                if (value >= 0) { price = value; }
                else { price = 0; }
            }
        }

        public override bool Equals(object obj)
        {
            return base.Equals(obj);
        }
        public override int GetHashCode()
        {
            return base.GetHashCode();
        }
        public override string ToString()
        {
            return Convert.ToString(price);
        }

    }
    class OrderService {
        public List<Order> ListOrder = new List<Order>();
        public void addOrder(int ID, string Item, string Client, int price){        //增加订单
            Order o = new Order();
            o.orderID = ID;
            o.od.Item.itemName = Item;
            o.od.user.clientName = Client;
            o.od.Price = price;
            bool same =false;
            foreach (Order n in ListOrder)
            {
                if (n.Equals(o))
                    same = true;
            }
            if (!same)
                ListOrder.Add(o);
        }
        public void deleteOrder(List<Order> ListDelete)                     {//删除订单
            if (ListOrder.Except(ListDelete).Count() < 1 || ListDelete.Count() == 0)
            {
                System.Console.WriteLine("订单删除失败");
                return;
            }
            ListOrder = ListOrder.Except(ListDelete).ToList();
        }

        public void rchangeOrder(List<Order> Listchange, int ID, string Itemname, string ClientN, int PriceN) {//修改订单
            if (ListOrder.Except(Listchange).Count() < 1 || Listchange.Count() == 0)
            {
                System.Console.WriteLine("订单修改失败");
                return;
            }
            ListOrder = ListOrder.Except(Listchange).ToList();
            foreach (Order n in Listchange)
            {
                n.orderID = ID;
                n.od.Item.itemName = Itemname;
                n.od.user.clientName = ClientN;
                n.od.Price = PriceN;
            }
            ListOrder = ListOrder.Union(Listchange).ToList();
        }

        public List<Order> searchByID(int id)            //通过单号查询
        {
            return ListOrder.Where(n => n.orderID == id).ToList().OrderBy(n => n.od.Price).ToList();
        }

        public List<Order> searchByItem(string name)               //商品查询
        {
            return ListOrder.Where(n => n.od.Item.itemName== name).ToList().OrderBy(n => n.od.Price).ToList();
        }

        public List<Order> searchByClient(string client)            //用户名查询
        {
            return ListOrder.Where(n => n.od.user.clientName.Equals(client)).ToList().OrderBy(n => n.od.Price).ToList();
        }

        public List<Order> searchByPrice(int price)                 //价格查询
        {   
            return ListOrder.Where(n => n.od.Price ==price).ToList().OrderBy(n => n.od.Price).ToList();
        }

        public void sort()                                                  //根据单号排序
        {
            ListOrder = ListOrder.OrderBy(n => n.orderID).ToList();
        }

        public void display()                                               //展示订单
        {
            foreach (Order n in ListOrder)
            {
               Console.WriteLine($"订单号：{n.orderID.ToString()} 客户：{n.od.user.ToString()} 金额：{n.od.ToString()} 商品名称：{n.od.Item.ToString()}");
            }
        }

    }
    class Client {
        public string client;
        public string clientName
        {
            get { return client; }
            set {
                client = value;
            }
        }

        public override string ToString()
        {
            return client;
        }
    }
    class OrderItem {
        public string item;
        public string itemName {
            get { return item; }
            set { item = value; }
        }
        public override string ToString()
        {
            return item;
        }
    }
}

